using System;

namespace Forecast.Domain.Implementation
{
    class Offer : IOffer
    {
        #region Variables

        private IPlay _creator;
        private IPredict _prediction;
        private int _numStocks;
        private int _price;
        private DateTime _expiryDate;
        private bool _isActive;

        #endregion
        #region Constructors

        public Offer(IPlay creator, IPredict prediction, int numStocks, int price, DateTime expiryDate)
        {
            if (expiryDate == DateTime.MinValue || expiryDate == DateTime.MaxValue)
                throw new ArgumentNullException("expiryDate");
            if (creator == null)
                throw new ArgumentNullException("creator");
            if (prediction == null)
                throw new ArgumentNullException("prediction");
            if (numStocks == 0)
                throw new ArgumentException("Need to offer at least one stock for sale.");
            if (numStocks < 0)
                throw new ArgumentException("Can't offer a negative amount of stocks for sale.");
            if (price == 0)
                throw new ArgumentException("Can't give the stocks away for free.");
            if (price < 0)
                throw new ArgumentException("Can't give money and stocks away at the same time.");
            if (expiryDate < DateTime.Today)
                throw new ArgumentException("Expiry date can't be in the past.");
            if (expiryDate > prediction.EndDate)
                throw new ApplicationException(String.Format("The new offer is set to expire on {0}, but {1} expires already on {2}",
                    expiryDate,
                    prediction.Ticker,
                    prediction.EndDate));
            
            _creator = creator;
            _prediction = prediction;
            _numStocks = numStocks;
            _price = price;
            _expiryDate = expiryDate;
            _isActive = true;

            Blackboard.OfferCreatedNow(this, _prediction);
        }

        #endregion
        #region Interface

        public void Cancel()
        {
            _isActive = false;
        }

        #endregion
        #region Properties

        public IPlay Creator
        {
            get { return _creator; }
        }

        public IPredict Prediction
        {
            get { return _prediction; }
        }

        public int NumStocks
        {
            get { return _numStocks; }
        }

        public int Price
        {
            get { return _price; }
        }

        public int TotalPrice
        {
            get { return _price * _numStocks; }
        }

        public DateTime ExpiryDate
        {
            get { return _expiryDate; }
        }

        public bool IsActive
        {
            get
            {
                if (_expiryDate < DateTime.Today)
                    return false;
                return _isActive;
            }
        }

        #endregion      
    }
}